package com.google.code.coma.domain;

import com.google.code.coma.space.Id;
import com.google.code.coma.space.SpaceEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Dominik Strasser, dominikstr@gmail.com
 */
@SpaceEntity("computer_")
public class Computer implements Serializable {
	@Id(generator = Id.GenerationType.AUTO_GENERATED)
	private String id;

	private List<Part> parts;

	private List<LifeCycleEntry> history = new ArrayList<LifeCycleEntry>();

	private Order order;

	public Computer(String worker, List<Part> parts) {
		this.parts = parts;
		history.add(new LifeCycleEntry(LifeCycleStep.CREATED, worker));
	}
	
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String getId() {
		return id;
	}

	public List<Part> getParts() {
		return parts;
	}

	public LifeCycleStep getCurrent() {
		return history.get(history.size() - 1).getStep();
	}

	public void changeState(LifeCycleStep state, String worker) {
		history.add(new LifeCycleEntry(state, worker));
	}

	public List<LifeCycleEntry> getHistory() {
		return Collections.unmodifiableList(history);
	}

	public class LifeCycleEntry implements Serializable {
		private LifeCycleStep step;

		private String worker;

		private Date date;

		public LifeCycleEntry(LifeCycleStep step, String worker) {
			this.step = step;
			this.worker = worker;
			this.date = new Date();
		}

		public LifeCycleStep getStep() {
			return step;
		}

		public String getWorker() {
			return worker;
		}

		public Date getDate() {
			return date;
		}
	}

	@Override
	public String toString() {
		return "Computer " + id;
	}
}
